package com.team.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.ContactDao;
import com.team.entity.Contact;
import com.team.entity.Project;
import com.team.util.Const;

public class NewContactServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	public void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		String method = request.getParameter("_m");
		
		if("save".equals(method)){
			save(request,response);
		}else if("edit".equals(method)){
			edit(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("findAll".equals(method)){  //
			//findAll(request,response);     //
		}else if("find".equals(method)){	
			findContact(request,response);		
			
		}else {
			response.sendError(404,"request a error path");
			return ;
		}
		
		
		
		
	}
	
/*	private void findAll(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		ContactDao dao = new ContactDao();
		List<Contact> listContact = dao.findAll();
		if(listContact != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_CONTACTLIST, listContact);
			response.sendRedirect("contactnote.jsp");
		}else{
			System.out.println("error");
		}
	}*/

	private void findContact(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String contanctId = request.getParameter("contactId");
		int id = Integer.parseInt(contanctId);
		ContactDao cd = new ContactDao();
		Contact ct = cd.selectByContactId(id);
		if(ct != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_CONTACT_RECORED, ct);
			response.sendRedirect("contact.jsp");
			return ;
		}else {
			System.out.println("联系人不存在");
			response.sendRedirect("contact.jsp");
			return;
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		String  id= request.getParameter("projId");
		System.out.println("projId"+id);
		int projId = Integer.parseInt(id);
		
		ContactDao dao = new ContactDao();
		
		List<Contact> contactList = dao.findAllByProjId(projId);
		
		if(contactList !=null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_CONTACT,contactList);
			response.sendRedirect("contact.jsp");
			return ;
		}else{
			System.out.println("error");
			response.sendRedirect("newContact.jsp");
			return ;
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		
	}

	
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String name = request.getParameter("name");
		String companyName=request.getParameter("company");
		String mobel=request.getParameter("mobel");
		String tel= request.getParameter("tel");
		String address=request.getParameter("address");
		String email=request.getParameter("email");
		String kind=request.getParameter("kind");
		String websate=request.getParameter("websate");
		String text=request.getParameter("text");
		String content=request.getParameter("textarea");
		String weibo=kind+"@"+text;
		
		
		HttpSession session = request.getSession();
		Project project =  (Project) session.getAttribute(Const.SESSION_PROJ);
		
		int projId = project.getId();
		Contact contact = new Contact();
		contact.setAddress(address);
		contact.setCompanyName(companyName);
		contact.setContactName(name);
		contact.setEmail(email);
		contact.setMobelNum(mobel);
		contact.setTelNum(tel);
		contact.setWebSate(websate);
		contact.setWeiBo(weibo);
		contact.setContent(content);
		contact.setProjId(projId);
		
	
		ContactDao dao = new ContactDao();
		int num = dao.save(contact);
		if(num > 0){
			System.out.println("save contact succ");
			response.sendRedirect("newContact.jspx?_m=select&projId="+projId);
			return ; 
			
		}else{
			System.out.println("save contact error");
			response.sendRedirect("newContact.jsp");
			return ;
		}
		
	}

}
